package ServerConnection;

import Client.ServerInfo;
import utils.FriendArrayList;

import javax.sql.DataSource;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.SQLException;

public class GetFriendArrayListServer implements Runnable {
    DatabaseServer databaseServer;

    GetFriendArrayListServer() {
        databaseServer = new DatabaseServer();
    }

    GetFriendArrayListServer(DatabaseServer dbs) {
        this.databaseServer = dbs;
    }


    public void run() {
        try {

            ServerSocket serverSocket = new ServerSocket(ServerInfo.SERVER_GETFRIENDARRAYLIST_PORT);
            System.out.println("返回用户好友列表服务成功启动！");
            Socket socket;
            while (true) {
                socket = serverSocket.accept();
                Thread handle = new Thread(new ThreadHandleGetFriendArrayList(socket, databaseServer));
                handle.start();
            }
        } catch (IOException e) {
            System.out.println("WARNING:AccountSignUpServer Init fail,Reason:socket has ioException.");
        }
    }
}
/**处理返回用户好友列表的线程类*/
 class ThreadHandleGetFriendArrayList implements Runnable{
    Socket socket = null;
    DatabaseServer databaseServer;

    public ThreadHandleGetFriendArrayList(Socket socket, DatabaseServer databaseServer) {
        super();
        this.socket = socket;
        this.databaseServer = databaseServer;
    }

    @Override
    public void run() {
        System.out.println("收到用户获取好友列表的请求，正在处理");
        //获取从客户端得到的消息，并且执行了消息。
        try {
            InputStream inputStream = socket.getInputStream();
            InputStreamReader inputStreamReader=new InputStreamReader(inputStream);//包装成字符流，提高效率
            BufferedReader bufferedReader=new BufferedReader(inputStreamReader);//缓冲区
            String userId = bufferedReader.readLine();//获得用户id

            String sql = "SELECT * FROM UserFriendList WHERE Uid1='"+userId+"' ;";
            ResultSet rs = databaseServer.returnResultSet(sql);
            FriendArrayList friendArrayList = new FriendArrayList();
            while (rs.next()){
                String userId1 = rs.getString("Uid1");
                String userId2 = rs.getString("Uid2");
                if(userId.equals(userId1)){
                    friendArrayList.add(userId2);
                }else {
                    friendArrayList.add(userId1);
                }
            }

            OutputStream outputStream = socket.getOutputStream(); //获取一个输出流，向服务端发送信息
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(friendArrayList);

            inputStream.close();
            objectOutputStream.close();
            outputStream.close();

        } catch (IOException | SQLException e) {
            e.printStackTrace();
        }
    }
}
class Test {
    public static void main(String[] args) {
        try {
            DatabaseServer databaseServer = new DatabaseServer();
            ServerSocket serverSocket = new ServerSocket(ServerInfo.SERVER_GETFRIENDARRAYLIST_PORT);
            System.out.println("返回用户好友列表服务成功启动！");
            Socket socket;
            while (true) {
                socket = serverSocket.accept();
                Thread handle = new Thread(new ThreadHandleGetFriendArrayList(socket, databaseServer));
                handle.start();
            }
        } catch (IOException e) {
            System.out.println("WARNING:AccountSignUpServer Init fail,Reason:socket has ioException.");
        }
    }
}